<template>
	<view class="box">
		<view style="padding: 10px; color: #A1A1A1; font-size: 14px;">
			说明：请输入充电柜支持最大功率，负载超过此功率，电柜会停充，并发送告警信息
		</view>
		<view class="uni-list">
			<view class="uni-list-cell" style="padding: 10px;">
				<view class="uni-list-cell-left" style="font-size: 15px;">
					总功率
				</view>
				<view class="uni-list-cell-db">
					<view class="uni-input">
						<view class="uni-input-wrapper">
							<input v-model="form.powerLimit" class="uni-input" type="digit" placeholder="请输入总功率(瓦)" />
							<text class="uni-icon">瓦</text>
						</view>
					</view>
				</view>
			</view>
			<view class="uni-list-cell" style="padding: 10px;">
				<view class="uni-list-cell-left" style="font-size: 15px;">
					过功率
				</view>
				<view class="uni-list-cell-db">
					<view class="uni-input">
						<view class="uni-input-wrapper">
							<input v-model="form.overPowerThreshold" class="uni-input" type="digit"
								placeholder="请输入过功率(瓦)" />
							<text class="uni-icon">瓦</text>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view style="padding: 30px 0px 0px 0px; margin: 40rpx 6%; text-align: center;">
			<button :loading="editLoading" @click="save()" type="primary" size="mini"
				style="margin-right: 20rpx; background-color: #4DA7FF; width: 47%; height: 80rpx; line-height: 80rpx;">保存</button>
			<button @click="back()" type="primary" size="mini"
				style="background-color: #C2C2C2; width: 47%; height: 80rpx; line-height: 80rpx;">取消</button>
		</view>
		<uni-popup ref="message" type="message">
			<uni-popup-message :type="msgType" :message="messageText" :duration="2000"></uni-popup-message>
		</uni-popup>
	</view>
</template>

<script>
	import {
		listParameterSending,
		equip,
	} from "@/api/equip.js";
	import {
		overPowerCode,
		normallyOpenCode,
		totalPowerCode,
		lockData
	} from '@/common/interface.js';
	export default {
		data() {
			return {
				form: {
					name:'test',
					powerLimit: '',
					overPowerThreshold: '',
				},
				optionBox: {},
				editLoading: false,
				msgType: '',
				messageText: '',
				equipid: '', //充电柜id
				code: '', //充电柜code
				lockData: lockData,
				changeIndex: '',
				templateBox:[],
			}
		},
		created() {
			this.templateBox = Object.keys(lockData).map((key, item) => {
				return {
					code: key,
					name: lockData[key]
				}
			});
		},
		onLoad(option) {
			this.equipid = option.equipid;
			this.code = option.code;
			this.optionBox = option;
			this.getDetail();
		},
		methods: {
			// 更换锁状态
			tempChange(e) {
				this.changeIndex = e.detail.value;
				this.form.lockStatus = this.templateBox[e.detail.value].code;
			},
			// 获取详情
			getDetail() {
				equip('/selectParams', 'get', {
						code: this.code,
						overPowerCode: overPowerCode,
						totalPowerCode: totalPowerCode,
						normallyOpenCode: normallyOpenCode
					}).then(res => {
					})
					.catch(err => {
						uni.hideLoading();
						err.forEach((item) => {
							if ('06040101' == item.protocolSemaphoreCode) {
								this.form.powerLimit = item.value;
							}
							if ('06042301' == item.protocolSemaphoreCode) {
								this.form.overPowerThreshold = item.value;
							}
						})
					})
			},
			// 保存
			save() {
				this.$Toast.loading('设置中···');
				let params = [{
					protocolSemaphoreCode: '06040101',
					value: this.form.powerLimit,
				}, {
					protocolSemaphoreCode: '06042301',
					value: this.form.overPowerThreshold,
				}];
				let filters= params.filter(item=>item.value);
				if (filters.length == 0) {
					this.$Toast.error('请输入参数值');
					return;
				}
				let datas = {
					chargingCabinetId: this.equipid,
					params: filters,
				};
				
				listParameterSending('/write', 'post', datas)
					.then((res) => {
						if (res.code == 200) {
							this.$Toast.success('参数设置成功');
							setTimeout(() => {
								this.back();
							}, 1500)
						}
					})
			},
			// 返回
			back() {
				let pages = getCurrentPages(); // 当前页面
				let page = getCurrentPages().pop(); // 当前页面
				let beforePage = pages[pages.length - 2]; // 上一页
				uni.navigateBack({
					success: function() {
						beforePage.onLoad(page.options); // 执行上一页的onLoad方法
					}
				});
			}
		}
	}
</script>

<style>
	.uni-input-wrapper {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		flex-wrap: nowrap;
		background-color: #FFFFFF;
	}

	.uni-input {
		height: 28px;
		line-height: 28px;
		font-size: 14px;
		padding: 0px;
		flex: 1;
		background-color: #FFFFFF;
	}

	.uni-icon {
		font-family: uniicons;
		font-size: 18px;
		font-weight: normal;
		font-style: normal;
		width: 36px;
		height: 36px;
		line-height: 26px;
		color: #000000;
	}

	.listBtn {
		width: 250rpx;
		height: 60rpx;
		line-height: 60rpx;
		float: left;
		text-align: center;
		color: #FFFFFF;
		background-color: #4DA7FF;
		border-radius: 5px;
	}

	/deep/ button::after {
		border: none;
	}
</style>
